package strategy

import "fmt"

//策略模式
//支付上下文
type Payment struct {
	context  *PaymentContext
	strategy PaymentStrategy
}
type PaymentStrategy interface {
	Pay(*PaymentContext)
}

func (p *Payment) Pay() {
	p.strategy.Pay(p.context)
}

//新建一个 payment 上下文分离
func NewPayment(name, cardid string, money int, strategy PaymentStrategy) *Payment {
	return &Payment{
		context: &PaymentContext{
			Name:   name,
			CardID: cardid,
			Money:  money,
		},
		strategy: strategy,
	}
}

type PaymentContext struct {
	Name, CardID string
	Money        int
}
type Cash struct {
}

func (c *Cash) Pay(ctx *PaymentContext) {
	fmt.Printf("Pay $%d to %s by cash", ctx.Money, ctx.Name)
}

type Bank struct {
}

func (b *Bank) Pay(ctx *PaymentContext) {
	fmt.Printf("Pay $%d to %s by bank carid: %s", ctx.Money, ctx.Name, ctx.CardID)
}

//策略模式知道了吗

type LoginContext struct {
	Username, Pwd string
	Salt          string
	EnType        string
}
type LoginStragy interface {
	Login(ctx *LoginContext)
}
type QQLogin struct {
}

func (Q QQLogin) Login(ctx *LoginContext) {
	panic("implement me")
}

type WangyiLogin struct {
}

func (w WangyiLogin) Login(ctx *LoginContext) {
	panic("implement me")
}

type Login struct {
	context  *LoginContext
	strategy LoginStragy
}

func NewLogin(username, pwd, salt, entype string, strategy LoginStragy) *Login {
	return &Login{
		context: &LoginContext{
			Username: username,
			Pwd:      pwd,
			Salt:     salt,
			EnType:   entype,
		},
		strategy: strategy,
	}
}
func (t *Login) LoginMain() {
	t.strategy.Login(t.context)
}
